package com.h6.crm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.h6.crm.annotation.LoginUser;
import com.h6.crm.annotation.ResponseResult;
import com.h6.crm.entity.Contract;
import com.h6.crm.entity.SysUser;
import com.h6.crm.service.ContractService;
import com.h6.crm.util.pojo.R;
import com.h6.crm.util.pojo.vo.QueryContractVO;
import com.h6.crm.util.pojo.vo.SaveContractByQuoteVO;
import com.h6.crm.util.pojo.vo.SaveContractVO;
import com.h6.crm.util.pojo.vo.UpdateContractStatusVO;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "合同管理", description = "合同管理的API")
@RestController
@RequestMapping("/contract")
@ResponseResult
public class ContractController {

  @Autowired
  ContractService contractService;

  @Operation(summary = "查询合同列表")
  @ApiResponses({
      @ApiResponse(responseCode = "200", content = {
          @Content(schema = @Schema(implementation = R.class), mediaType = "application/json") }) })
  @PostMapping("/list")
  public IPage<Contract> queryContractList(@Valid @RequestBody QueryContractVO contractVo) {
    return contractService.queryContractList(contractVo);
  }

  @Operation(summary = "保存合同")
  @ApiResponses({
      @ApiResponse(responseCode = "200", content = {
          @Content(schema = @Schema(implementation = R.class)) }) })
  @PostMapping(value = "/save")
  public Boolean saveContract(@Valid @RequestBody SaveContractVO contractVo,
      @Parameter(hidden = true) @LoginUser SysUser user) {
    return contractService.saveContract(contractVo, user);
  }

  @Operation(summary = "根据报价单生成合同")
  @ApiResponses({
      @ApiResponse(responseCode = "200", content = {
          @Content(schema = @Schema(implementation = R.class), mediaType = "application/json") }) })
  @PostMapping("/quote/save")
  public Boolean saveContractByQuote(@Valid @RequestBody SaveContractByQuoteVO contractVo,
      @Parameter(hidden = true) @LoginUser SysUser user) {
    return contractService.saveContractByQuote(contractVo, user);
  }

  @Operation(summary = "根据ID删除合同")
  @ApiResponses({
      @ApiResponse(responseCode = "200", content = {
          @Content(schema = @Schema(implementation = R.class), mediaType = "application/json") }) })
  @PostMapping("/{contractId}/delete")
  public Boolean delete(
      @Parameter(description = "合同ID", required = true) @PathVariable("contractId") String contractId) {
    return contractService.removeByIdAndUpdateProject(contractId);
  }

  @Operation(summary = "查询合同详细信息")
  @ApiResponses({
      @ApiResponse(responseCode = "200", content = {
          @Content(schema = @Schema(implementation = R.class), mediaType = "application/json") }) })
  @PostMapping("/{contractId}")
  public Contract getContractDetail(
      @Parameter(description = "合同ID", required = true) @PathVariable("contractId") String contractId) {
    return contractService.getContractDetailById(contractId);
  }

  @Operation(summary = "修改合同状态/付款状态")
  @ApiResponses({
      @ApiResponse(responseCode = "200", content = {
          @Content(schema = @Schema(implementation = R.class), mediaType = "application/json") }) })
  @PostMapping("/{contractId}/status")
  public Boolean updateContractStatus(@RequestBody UpdateContractStatusVO contractVo,
      @Parameter(description = "合同ID", required = true) @PathVariable("contractId") String contractId) {
    return contractService.updateContractStatus(contractId, contractVo);
  }

  @Operation(summary = "统计指定时间段的金额")
  @ApiResponses({
      @ApiResponse(responseCode = "200", content = {
          @Content(schema = @Schema(implementation = R.class), mediaType = "application/json") }) })
  @PostMapping("/calculate")
  public Map<String, String> calculateAmountOfperiod(@RequestBody QueryContractVO contractVo) {
    return contractService.calculateAmountOfperiod(contractVo);
  }

}
